package org.apache.cassandra.server;

import org.apache.cassandra.server.impl.CassandraNode;
import org.apache.cassandra.server.impl.CassandraNodeFactory;
import org.eclipse.core.runtime.Plugin;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The activator class controls the plug-in life cycle
 */
public class CassandraNodeActivator extends Plugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "org.apache.cassandra.server";

	// The shared instance
	private static CassandraNodeActivator plugin;

	private CassandraNode node;

	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * The constructor
	 */
	public CassandraNodeActivator() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		logger.info("starting cassandra server plugin....");
		CassandraNodeFactory factory = new CassandraNodeFactory();
		node = factory.create();
		node.start();
		logger.info("cassandra server plugin started.");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		node.shutdown();
		logger.info("cassandra server plugin stopped");
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static CassandraNodeActivator getDefault() {
		return plugin;
	}

}
